#!/bin/bash

#####################
# Date: 2019/05/07  #
# Author: lijunye   #
# Version: 0.1      #
#####################

# ep: bash -c "$(curl https://raw.githubusercontent.com/xmapst/nginx/master/Nginx_install.sh --silent)"

[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
sed -i "s/SELINUX=enforcing/SELINUX=disabled/"  /etc/selinux/config
setenforce 0
which  ntpdate
if [ $? -eq 0 ];then
    /usr/sbin/ntpdate time1.aliyun.com > /dev/null 2>&1
    echo "*/5 * * * * /usr/sbin/ntpdate -s time1.aliyun.com > /dev/null 2>&1">>/var/spool/cron/root    
else
    yum install ntpdate -y
    /usr/sbin/ntpdate time1.aliyun.com
    echo "*/5 * * * * /usr/sbin/ntpdate -s time1.aliyun.com > /dev/null 2>&1">>/var/spool/cron/root    
fi
mv -f /root/software /root/software_$(date -d "2 second" +"%Y_%m_%d_%H_%M_%S")
clear

progressfilt()
{
    local flag=false c count cr=$'\r' nl=$'\n'
    while IFS='' read -d '' -rn 1 c
    do
        if $flag
        then
            printf '%c' "$c"
        else
            if [[ $c != $cr && $c != $nl ]]
            then
                count=0
            else
                ((count++))
                if ((count > 1))
                then
                    flag=true
                fi
            fi
        fi
    done
}

function init(){
    echo "###################################"
    echo "Install Dependency package"
    yum install -y bash-completion gcc glib gcc-c++ make re2c curl curl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel libmcrypt libmcrypt-devel zlib zlib-devel openssl* openssl-devel freetype freetype-devel gd gd-devel perl perl-devel ncurses ncurses-devel bison bison-devel libtool gettext gettext-devel cmake bzip2 bzip2-devel pcre pcre-devel libaio* python-devel patch

    if [ $? -ne 0 ];then
        echo -e "\033[41;37m Install Dependency package ERROR\033[0m"
        exit 1
    fi

    echo "###################################"
    echo "Download the installation package and dependencies from"
    software_list="nginx-1.16.0.tar.gz echo-nginx-module.tar.gz lua-nginx-module-v0.10.13.tar.gz luajit-2.0.tar.gz lua-upstream-nginx-module.tar.gz nginx_upstream_check_module.tar.gz nginx-sticky.tar.gz ngx_cache_purge-2.3.tar.gz ngx_devel_kit-v0.3.1rc1.tar.gz ngx_python.tar.gz openssl-1.1.1a.tar.gz pcre-8.42.tar.gz"
    
    mkdir -p /root/software
    cd /root/software
    for i in $software_list;do
    wget --progress=bar:force https://github.com/xmapst/nginx/releases/download/1.0/$i 2>&1 | progressfilt &&
        if [ -f "$i" ];then
            continue
        else
            echo -e "\033[41;37m Downlaod ERROR\033[0m"
            echo -e "\033[41;37m wget --progress=bar:force https://github.com/xmapst/nginx/releases/download/1.0/$i\033[0m"
            exit 1
        fi
    done
    cd ~/
}

function nginx_install(){
    init

    echo "###################################"
    echo "Start installation Nginx"
    Install_patch=$1
    Install_parameter=$2

    cd /root/software
    for tar in *.tar.gz;  do tar xvf $tar; done &&
    
    cd luajit-2.0 && make && make install && cd ../

    cd nginx-1.16.0
    patch -p1 < ../nginx_upstream_check_module/check_1.14.0+.patch

    export LUAJIT_LIB=/usr/local/lib
    export LUAJIT_INC=/usr/local/include/luajit-2.0
    export PYTHON_INC=/usr/include/python2.7
    export PYTHON_BIN=/usr/bin

    useradd -s /sbin/nologin www

    ./configure --prefix=${Install_patch} --user=www --group=www --error-log-path=${Install_patch}/logs/error.log --http-log-path=${Install_patch}/logs/access.log --pid-path=${Install_patch}/logs/nginx.pid ${Install_parameter}
    make && make install &&

    cat <<EOF >/etc/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=${Install_patch}/logs/nginx.pid
ExecStartPre=${Install_patch}/sbin/nginx -t -c ${Install_patch}/conf/nginx.conf
ExecStart=${Install_patch}/sbin/nginx -c ${Install_patch}/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target
EOF

    if [ -a ${Install_patch}/sbin/nginx ];then        
        ${Install_patch}/sbin/nginx -t

        if [ $? -ne 0 ];then
            echo -e "\033[41;37m Nginx Configure Check failed! \033[0m"
            echo -e "\033[41;37m Manually execute the following command processing! \033[0m"
            echo "$${Install_patch}/sbin/nginx -t"
            exit 1
        fi
        systemctl daemon-reload
        systemctl enable nginx
        systemctl restart nginx

        echo -e "\033[42;37m Nginx Installed \033[0m"
        echo -e "\033[42;37m Install dir ${Install_patch} \033[0m"
    fi
}

clear
echo "##########################################"
echo "#       Auto Install Ningx.             ##"
echo "#       Press Ctrl + C to cancel        ##"
echo "#       Any key to continue             ##"
echo "##########################################"
echo "(1) Install Nginx Proxy"
echo "(2) Install Nginx Web"
echo "(4) EXIT"
read -p "Please input your choice:" NUM
case $NUM in 
1)
    Install_patch=/usr/local/webserver/nginx
    Install_parameter="--with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-stream --with-openssl=../openssl-1.1.1a --with-pcre=../pcre-8.42 --with-http_image_filter_module --add-module=../nginx-sticky --add-module=../ngx_cache_purge-2.3 --add-module=../nginx_upstream_check_module --add-module=../echo-nginx-module --add-module=../ngx_devel_kit --add-module=../lua-nginx-module --add-module=../lua-upstream-nginx-module --add-module=../ngx_python --with-ld-opt='-Wl,-rpath,/usr/local/lib'"
    nginx_install $Install_patch "$Install_parameter"
    ;;
2)
    Install_patch=/usr/local/nginx
    Install_parameter="--with-pcre=../pcre-8.42 --with-openssl=../openssl-1.1.1a --add-module=../ngx_python --add-module=../nginx-sticky --add-module=../ngx_devel_kit --add-module=../lua-nginx-module --add-module=../echo-nginx-module --add-module=../nngx_cache_purge-2.3 --add-module=../lua-upstream-nginx-module --add-module=../nginx_upstream_check_module --with-stream --with-file-aio --with-http_v2_module --with-http_ssl_module --with-mail_ssl_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_slice_module --with-http_gunzip_module --with-http_realip_module --with-mail --with-threads --with-http_addition_module --with-http_secure_link_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_random_index_module --with-http_auth_request_module --with-http_image_filter_module --with-ld-opt='-Wl,-rpath,/usr/local/lib'"
    nginx_install $Install_patch "$Install_parameter"
    ;;
3)
    echo -e "\033[41;37m You choice channel! \033[0m" && exit 0
    ;;
*)
    echo -e "\033[41;37m Input Error! Place input{1|2|3} \033[0m" && exit 1
    ;;
esac
